<include file="Public/header"/>
<include file="Public/menu"/>
<style type="text/css">
.edit-batch-list .ebatch-name{
	width:480px;
}
.edit-batch-list .ebatch-txt{
	width:480px;
}
</style>
<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            营销
            <small>管理</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="{:U('GoodsPromotion/index')}"><i class="fa fa-dashboard"></i> 营销</a></li>
            <li class="active">商品促销</li>
        </ol>
    </section>

    <!-- 内容 -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">新增商品促销</h3>

                        <div class="box-tools">

                            <div class="btn-group pull-right" style="margin-right: 10px">
                                <a href="{:U('GoodsPromotion/index')}" class="btn btn-sm btn-default"><i
                                        class="fa fa-list"></i>&nbsp;订单促销列表</a>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form id="form" action="{:U('GoodsPromotion/add')}" method="post" accept-charset="UTF-8" class="form-horizontal">
                        <div class="box-body">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">规则名称</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <input name="name" type="text" class="form-control" placeholder="名称" value="" />
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">规则描述</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <textarea name="description" class="form-control" placeholder="描述"></textarea>
                                    </div>
                                </div>
                            </div>

                            <input name="paita" value="1" type="hidden" class="" checked>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">启用状态</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        否<input name="display" value="0" type="radio" class="" >
                                        是 <input name="display" value="1" type="radio" class="" checked>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">排序</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <input name="sort" value="" class="form-control ajax-sort" type="text" placeholder="越小越靠前">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">促销条件</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <select class="form-control  select2-hidden-accessible select_option"
                                                name="type_question">
                                            <option value="1" selected="selected">指定商品</option>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group question_select" style=" ">
                                <label class="col-sm-2 control-label">设置商品</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <button type="button"  class="btn btn-default pull-left" id="daterange-btn">
                                            <span>
                                              <i class="fa fa-calendar"></i> 点击添加商品
                                            </span>
                                            <i class="fa fa-caret-down"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group question_select" style="display: block">
                                <div class="col-sm-offset-2 col-sm-6">
                                    <div class="box box-primary  col-sm-6" >
                                        <!-- /.box-header -->
                                        <div class="box-body">
                                            <ul class="todo-list ui-sortable edit-batch-list"></ul>
                                        </div>
                                        <input id="yc-goods-ids" name="question_goods_id_array" type="hidden"/>
                                        <!-- /.box-body -->
                                    </div>
                                </div>
                            </div>

                            <!--促销结果-->
                            <div class="form-group">
                                <label class="col-sm-2 control-label">促销结果</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <select class="form-control result-select"name="type_result">
                                            <option value="" selected="selected">请选择</option>
                                            <option value="1" >符合条件的指定商品自定义促销价</option>
                                            <option value="2">符合条件的指定商品减去固定价格</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group result_price" style="display: none">
                                <label class="col-sm-2 control-label">促销中减去</label>
                                <div class="col-sm-6">
                                    <div class="input-group">
                                        <input class="form-control  select2-hidden-accessible" id="cuxiao" name="result_price">
                                        <span class="input-group-addon">元</span>
                                    </div>
                                </div>
                            </div>
                            <!--促销结果底部-->
                            <div class="form-group">
                                <label class="col-sm-2 control-label">开始时间</label>
                                <div class="col-sm-6">
                                    <div class="input-group">
                                        <input id="start_time" name="start_time" class="form-control" type="text" value="" readonly />
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">结束时间</label>
                                <div class="col-sm-6">
                                    <div class="input-group">
                                        <input id="end_time" name="end_time" class="form-control" type="text" value="" readonly />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->
                        <div class="box-footer">
                            <div class="col-sm-2"></div>
                            <div class="col-sm-6">
                                <div class="btn-group pull-right">
                                    <button type="submit" class="btn btn-info pull-right submit_click">提交</button>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-footer -->
                    </form>
                </div>

            </div>
        </div>

    </section>
</div>


<!-- 底部 -->
<include file="Public/common_footer"/>
<script src="/Public/layer/layer.js"></script>
<link rel="stylesheet" href="/Public/admin/jedate/jedate.css" />
<script src="/Public/admin/jedate/jedate.js"></script>
<script type="text/javascript">
    // 设置时间
    function setJedate(){
        //不显示秒的解决办法
        var stxt = $("#start_time").val().trim();
        if(stxt.length == 19){
            $("#start_time").val(stxt.substr(0, 16));
        }
        var etxt = $("#end_time").val().trim();
        if(etxt.length == 19){
            $("#end_time").val(etxt.substr(0, 16));
        }
        var start = {
            dateCell: '#start_time',
            format: 'YYYY-MM-DD hh:mm',
            minDate: '1970-01-01 00:00:00',
            festival: false,
            maxDate: '2099-12-31 23:59:59',
            isinitVal:true,
            isTime: true,
            choosefun: function(datas) {
                end.minDate = datas; 
            }
        };
        var end = {
            dateCell: '#end_time',
            format: 'YYYY-MM-DD hh:mm',
            minDate: '1970-01-01 00:00:00',
            festival: false,
            maxDate: '2099-12-31 23:59:59',
            isinitVal:true,
            isTime: true,
            choosefun: function(datas) {
                start.maxDate = datas;
            }
        };
        jeDate(start);
        jeDate(end);
    }
    setJedate();
    var ychser = new YcChoser();
    function delGoodsItem(o) {
        ychser.isRepeat($(o).attr("data-id"));
        ychser.remove();
        $("#yc-goods-ids").val(ychser.getChosenIds());
        $(o).parents("li").remove();
    }
    function updateGoodsIds(ids) {
        $("#yc-goods-ids").val(ids);
        ychser.setChosenIds(ids);
    }
    $('#daterange-btn').click(function () {
        layer.open({
            type: 2,
            title: '添加商品',
            area: ['1400px', '830px'],
            fixed: false,
            maxmin: true,
            content: "{:U('productAdd','' , '')}"
        });

    });

    $('.select_option').change(function () {
        var v = $(this).val();
        if (v == 1) {
            $('.result_price_one').prop('type','text')
            $('.question_man_price_two').hide();
        }
        if (v == 2) {
            $('.result_price_one').prop('type','hidden')
            $('.question_man_price_two').show();
        }
    });
    $('.result-select').change(function () {
        var v = $(this).val();
        if (v == 1) {
            $('.result_price_one').show();
            $('.result_price').hide();
        }
        if (v == 2) {
            $('.result_price').show();
            $('.result_price_one').hide();
        }
    });

    $(".todo-list").delegate(".is_price","blur",function()
    {
        var getFloatStr = function(num){
            num += '';
            num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符

            if(/^0+/) //清除字符串开头的0
                num = num.replace(/^0+/, '');
            if(!/\./.test(num)) //为整数字符串在末尾添加.00
                num += '.00';
            if(/^\./.test(num)) //字符以.开头时,在开头添加0
                num = '0' + num;
            num += '00';        //在字符串末尾补零
            num = num.match(/\d+\.\d{2}/)[0];
            return num;
        };
        var num = parseFloat($(this).val());
        var numprice = parseFloat(getFloatStr(num))
        var price = $(this).attr('prices');
//        if(num==''){
//            alert('促销价格不能为空!')
//            return false;
//        }
        if(numprice>=price){
            alert('促销价不能大于等于售价')
            $(this).val(price-0.01);
            return false;
        }



    })


    $("#form").validate({
        ignore: "",
        rules: {
            name:{
                required: true,
                rangelength:[2, 50]
            },
			question_man_price_two:{
				isMoney:true,
			},
			result_price:{
				isMoney:true
			},
            start_time:{
                required: true,
                isActTime: true
            },
            end_time:{
                required: true,
                isActTime: true,
                dateTimeBigThan: "#start_time"
            }
        },
        messages:{
            name:{
                required: "请输入名称"
            },
            start_time:{
                required: "请输入开始时间"
            },
            end_time:{
                required: "请输入结束时间",
                dateTimeBigThan: "【注意：】结束时间不能小于开始时间"
            }
        },
        submitHandler: function(form){
            form.submit();   //提交表单
        }
    }); 
</script>
